package defErr

import (
	"admin100/library/define"
	"admin100/library/define/defLang"

	"github.com/gogf/gf/errors/gerror"
	"github.com/gogf/gf/text/gstr"
)

func IfPanic(err error, code define.ErrorCodeType, msg ...string) {
	if err != nil {
		if len(msg) > 0 {
			Panic(code, msg[0])
		} else {
			Panic(code)
		}
	}
}

// 判断是否有错误且查询的数据是否为空
func SelectRetPanic(err error, data interface{}, code define.ErrorCodeType) {
	if err != nil {
		Panic(code, err.Error())
	} else if data == nil {
		Panic(define.RecordNodFound)
	}
}

// 指定的msg，即不等于err.code所指向的msg返回给客户端
func Panic(code define.ErrorCodeType, msg ...string) {
	//默认
	gErr := gerror.NewCode(int(code), defLang.GotMsg(code)).(*gerror.Error)
	if len(msg) > 0 && gstr.Trim(msg[0]) != "" {
		panic(gerror.NewCode(int(code), msg[0]).(*gerror.Error))
	} else {
		panic(gErr)
	}
}
